<?php

defined('SYSPATH') OR die('No direct access allowed.');

class Controller_Administration extends Controller_Initialize {

  public $secure_actions = array(
      // user actions
      'index' => 'admin',
      'gallery' => 'admin',
      'articles' => 'admin',
  );

  public function before() {

    parent::before();

    $this->template->navigation->active = 'admin';

  }

  public function action_index() {
    
    if (!empty($_POST)) {

      $model = null;
      $model = ORM::factory('system_settings')->find(1);
      
      if (isset($_POST['online_state'])) {
        $model->online = $_POST['online_state'];
      }
      if (isset($_POST['photo_newest'])) {
        $model->photo_newest = $_POST['photo_newest'];
      }
      if (isset($_POST['photo_popular'])) {
        $model->photo_popular = $_POST['photo_popular'];
      }
      if (isset($_POST['articles'])) {
        $model->articles = $_POST['articles'];
      }
      echo kohana::debug($model->save());
      
      if ($model->saved()) {
        Request::instance()->redirect('administration');
      }

    }

    $view = View::factory('templates/administration/index')
      ->bind('system_settings', $system_settings);

    $system_settings = ORM::factory('system_settings')->find(1);

    $this->template->content = $view;

  }

  public function action_gallery() {

    $view = View::factory('templates/administration/gallery')
      ->bind('photo_user', $photo_users);

    $photo_users = ORM::factory('photo_user')->order_by('username')->order_by('date', 'DESC')->find_all();

    $this->template->content = $view;

  }

  public function action_articles() {

    $this->template->content = View::factory('templates/administration/articles')
                    ->bind('articles', $articles);

    $articles = ORM::factory('article')->find_all();

  }

  public function action_users() {

    $this->template->content = View::factory('templates/administration/users')
                    ->bind('users', $users);

    $users = ORM::factory('user')->find_all();

  }

  public function action_blogs() {

    $this->template->content = View::factory('templates/administration/blogs')
                    ->bind('blogs', $blogs);

    $blogs = ORM::factory('blog')->find_all();

  }

  public function action_discussions() {

    $this->template->content = View::factory('templates/administration/discussions')
                    ->bind('discussions', $discussions);

    $discussions = ORM::factory('discussion')->find_all();

  }

  public function action_stats() {

    Request::instance()->redirect('stats');

  }

}